"""
用例ID: TC_login_001
用例标题: 验证输入未注册的用户可以登录系统
预制条件:
    1. 在系统中 未注册用户名为:junfu 密码为:123456 的用户并且要注册的邮箱不重复
    2. 打开谷歌浏览器,进入ecshop的登录页面
用例步骤:
    1. 输入用户名 : junfu
    2. 输入email : 123@qq.com
    3. 输入密码 : 123456
    4. 输入确认密码 : 123456
    5. 输入手机号 : 11111111111
    6. 点击注册
预期结果:
    页面左上角显示登录的用户名
"""
import unittest
import os
import ddt
from Page.register_page import *
from common.databaseTool import DatabaseTool
from common.openExcel import *

filePath = os.path.dirname(__file__) + "/testData/测试数据.xlsx"
table = Open_excel(filePath, "注册成功",
                   {"password": str, "confirm_password": str, "email": str, "qq": str, "office_tel": str,
                    "home_tel": str, "phone": str})
dictData = table.read()


@ddt.ddt
class RegisterCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # 预制条件:
        #     1. 在系统中没有用户名为:judy 或 邮箱为123@qq.com 的用户
        cls.db = DatabaseTool("168168", "ecshop_db", port=3308)

    def setUp(self):
        #     2. 打开谷歌浏览器,进入ecshop的注册页面
        driver = open_browser()
        self.lg = Register_page(driver)
        self.lg.open_url(login_url)
        sql = "delete from ecs_users where user_name=judy"
        self.db.execute_one(sql)
        sql = "delete from ecs_users where email='123@qq.com'"
        self.db.execute_one(sql)

    def tearDown(self):
        # 关闭浏览器
        self.lg.quit()

    @classmethod
    def tearDownClass(cls):
        # 还原数据库
        sql = "delete from ecs_users where user_name='judy'"
        cls.db.execute_one(sql)

    def register(self, username, email, password, confirm_password, qq, office_tel, home_tel, phone, question, answer):
        # 用例步骤:
        # 输入用户名
        self.lg.input_username(username)
        # 输入email
        self.lg.input_email(email)
        # 输入密码
        self.lg.input_password(password)
        # 输入确认密码
        self.lg.input_confirm_password(confirm_password)
        # 输入qq号
        self.lg.input_qq_number(qq)
        # 输入办公电话
        self.lg.input_office_tel(office_tel)
        # 输入家庭电话
        self.lg.input_home_tel(home_tel)
        # 输入手机号
        self.lg.input_extend_field5(phone)
        # 选择密码问题
        self.lg.question(question)
        # 输入密码问题答案
        self.lg.answer(answer)
        # 点击注册
        self.lg.click_login()
        # 预期结果:
        #     页面左上角显示登录的用户名并且数据库已存在用户数据
        sql = "select user_name from ecs_users"
        data = self.db.fetchAll(sql)
        users = [i['user_name'] for i in data]
        try:
            user_url = self.lg.get_element_text(("class name", "f4_b"))
            self.assertEqual(username, user_url, msg="执行失败,当前用户名与预期不一致")
            self.assertIn(username, users, msg="执行失败,数据库中未找到注册的用户信息")
        except Exception as e:
            raise Exception("用例执行失败,当前页面未定位到注册的用户名")

    @ddt.data(*dictData)
    def test_register(self, data):
        # print(data)
        self.register(**data)


if __name__ == '__main__':
    unittest.main()
